package swc.smu.ui.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * Created by smu on 2016/2/2.
 */
public class CircleImageView extends ImageView {

   static Paint paint = new Paint();
    //圆形以外区域颜色（应与空白区域颜色相同，空白区颜色）
    int backColor, ringColor;
    //圆形半径
    int radius = 0;
    //是否需要对圆形进行描边
    boolean isEdge = false;
    //圆形边缘厚度
    int thick = 0;

    int width, height;
    int innerRadius;
    int outterRadius;

    public CircleImageView(Context context) {
        super(context);
        paint.setColor(Color.parseColor("#00000000"));
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        decorateToCircle(canvas);
    }

    /**
     * 将图片装饰成圆形
     * @param canvas
     */
    private void decorateToCircle(Canvas canvas) {
        width = canvas.getWidth();
        height = canvas.getHeight();
        if(radius > 0) {
            innerRadius = radius;
        } else {
            innerRadius = width > height ? height / 2 : width / 2;
        }
        outterRadius =(int) (Math.ceil(Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2))) / 2);

        //设置绘
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(outterRadius - innerRadius);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(width / 2, height / 2, innerRadius  + (outterRadius - innerRadius) / 2, paint);
        if(isEdge) {
            drawImageEdge(canvas);
        }
    }

    private void drawImageEdge(Canvas canvas) {
        paint.setColor(ringColor);
        paint.setStrokeWidth(thick);
        canvas.drawCircle(width / 2, height / 2, innerRadius - thick / 2, paint);
    }

//
//    private Bitmap paseDrawableToBitmap(Drawable drawable) {
//
//        if(drawable == null) {
//            return null;
//        } else if(drawable instanceof BitmapDrawable) { //直接获取Bitmap
//            return ((BitmapDrawable) drawable).getBitmap();
//        } else if(drawable instanceof ColorDrawable) {
//            return null;
//        } else {
//            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicWidth(), Bitmap.Config.ARGB_8888);
//            Canvas canvas = new Canvas(bitmap);
//            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
//            drawable.draw(canvas);
//            return bitmap;
//        }
//    }


    public int getBackColor() {
        return backColor;
    }

    /**
     * 设置空白区域颜色，与整体背景色统一
     * @return
     */
    public void setBackColor(int backColor) {
        this.backColor = backColor;
    }

    public int getRingColor() {
        return  ringColor;
    }

    /**
     * 设置环形边缘颜色
     * @param ringColor
     */
    public void setRingColor(int ringColor) {
        this.ringColor = ringColor;
    }

    public int getRadius() {
        return radius;
    }

    /**
     * 圆形的半径
     * @return
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    public boolean isEdge() {
        return isEdge;
    }

    /**
     * 是否需要对圆形进行描边
     * @param isEdge
     */
    public void setIsEdge(boolean isEdge) {
        this.isEdge = isEdge;
    }

    public int getThick() {
        return thick;
    }

    /**
     * 设置描边边缘的厚度
     * @param thick
     */
    public void setThick(int thick) {
        this.thick = thick;
        if(!isEdge) {
            setIsEdge(true);
        }
    }
}
